package com.orange.lswidget.widgets.musicplayer;

import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import com.orange.lswidget.widgets.musicplayer.entry.MusicPlayer;
import com.orange.lswidget.widgets.musicplayer.factory.AcerMPFactory;
import com.orange.lswidget.widgets.musicplayer.factory.DefaultMPFactory;
import com.orange.lswidget.widgets.musicplayer.factory.IMPFactory;
import com.orange.lswidget.widgets.musicplayer.factory.SamSungMPFactory;
import com.orange.lswidget.widgets.musicplayer.service.IMusicPlayerService;
import com.orange.util.Constants;
import com.orange.util.LSServiceUtils;
import com.orange.util.LogHelper;


public final class MusicPlayerManager {
	private static final String			TAG						= "MusicPlayerManager";

	private static final String			GOOGLE_PACKAGE_NAME		= "com.google.android.music";
	private static final String			DEFAULT_PACKAGE_NAME	= "com.android.music";
	private static final String			DEFAULT_CLASS_NAME		= "com.android.music.MediaPlaybackService";
	private static final String			ACER_PACKAGE_NAME		= "com.android.music";
	private static final String			ACER_CLASS_NAME			= "com.android.music.MediaPlaybackService";
	private static final String			SAMSUNG_PACKAGE_NAME	= "com.android.music";
	private static final String			SAMSUNG_CLASS_NAME		= "com.android.music.player.service.CorePlayerService";

	// private static final String MODEL_SAMSUNG = "samsung";
	private static final String			MODEL_SAMSUNG			= "gt-i9001";
	private static final String			MODEL_ACER				= "acer";
	private static final String			MODEL_HTC				= "htc";

	private static Object				sObject					= new Object();
	private static MusicPlayer			sMpInstance;
	private static MusicPlayerManager	sMusicPlayerManager;
	private static IMPFactory			sFactory;
	private static String				sBuildModel;

	private Context						mContext;

	private MusicPlayerManager(Context context) {
		mContext = context;
		sBuildModel = Build.MODEL.toLowerCase(Locale.UK);
	}

	public static MusicPlayerManager getInstance(Context context) {
		if (sMusicPlayerManager == null) {
			synchronized (sObject) {
				if (sMusicPlayerManager == null) {
					sMusicPlayerManager = new MusicPlayerManager(context);
				}
			}
		}
		return sMusicPlayerManager;
	}

	public MusicPlayer createMusicPlayer() {
		LogHelper.v(TAG, "createMusicPlayer. Build.MODEL: " + sBuildModel);

		if (sBuildModel.contains(MODEL_SAMSUNG)) {
			sFactory = new SamSungMPFactory();
		} else if (sBuildModel.contains(MODEL_ACER)) {
			sFactory = new AcerMPFactory();
//		} else if (MusicUtils.isPackageExist(mContext, GOOGLE_PACKAGE_NAME)) {
//			sFactory = new GoogleMPFactory();
		} else { // Otherwise, default.
			sFactory = new DefaultMPFactory();
		}

		sMpInstance = sFactory.createMusicPlayer(mContext);
		return sMpInstance;
	}

	/**
	 * get MusicPlayerbackService
	 * @param service
	 * @return
	 */
	public IMusicPlayerService createMpService(IBinder service) {
		LogHelper.v(TAG, "createMpService. Build.MODEL: " + sBuildModel);

		IMusicPlayerService mpService;
		if (sBuildModel.contains(MODEL_SAMSUNG)) {
			sFactory = new SamSungMPFactory();
		} else if (sBuildModel.contains(MODEL_ACER)) {
			sFactory = new AcerMPFactory();
		} else {
			sFactory = new DefaultMPFactory();
		}

		mpService = sFactory.createMpService(service);
		return mpService;
	}

	public void startMusicPlayerActivity(Context context, long albumId) {
		LogHelper.i(TAG, "startMusicPlaybackActivity. albumId: " + albumId
				+ ",mBuildModel is: " + sBuildModel);

		try {
			Intent intent = new Intent();
			if (sBuildModel.startsWith(MODEL_HTC)) {
				intent.setAction(Constants.ACTION_MUSIC_PLAYBACK_HTC_VIEWER);
			}
			// add by gaoge 2011-09-02,fix bug on acer when double click music
			// panel
			else {
				if (albumId >= 0) {
					intent.setAction(Constants.ACTION_MUSIC_PLAYBACK_VIEWER);
				} else {
					intent.setAction(Constants.ACTION_MUSIC_PLAYBACK_BROWSER);
				}
			}
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
					| Intent.FLAG_ACTIVITY_CLEAR_TOP);

			LSServiceUtils.startActivity(context, intent);
		} catch (Exception e) {
			LogHelper.e(TAG, "startMusicPlayerActivity. ", e);
		}
	}

	public String getMpGooglePkgName() {
		return GOOGLE_PACKAGE_NAME;
	}

	// This null could cause exception when there is a google music player package.
	public String getMpGoogleClsName() {
		// XXX: Must not be null.
		return null;
	}

	public String getMpAcerPkgName() {
		return ACER_PACKAGE_NAME;
	}

	public String getMpAcerClsName() {
		return ACER_CLASS_NAME;
	}

	public String getMpSamSungPkgName() {
		return SAMSUNG_PACKAGE_NAME;
	}

	public String getMpSamSungClsName() {
		return SAMSUNG_CLASS_NAME;
	}

	public String getMpDefaultPkgName() {
		return DEFAULT_PACKAGE_NAME;
	}

	public String getMpDefaultClsName() {
		return DEFAULT_CLASS_NAME;
	}
}
